﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.IdentityModel.Claims;
using System.Web;

using bdtheque.data.poco;
using bdtheque.data.dataaccess;
using bdtheque.business.interfaces;
using bdtheque.data.interfaces;
using Microsoft.IdentityModel.Web;
using System.Web.UI;

namespace bdtheque.business.bo
{
    public static class LoginManager
    {
        public static void UserLookUp(Page page, IUnitOfWork unitOfWork)
        {
            // Only do this if the user was not already looked up and set
            if (SessionFacade.UserId == null)
            {
                // Access Claims and retrieve unique nameidentifier and identityprovider
                IWIFHelper wifHelper = new WIFHelper((IClaimsPrincipal)HttpContext.Current.User);
                string nameidentifier = wifHelper.GetClaimValue("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier");
                string identityprovider = wifHelper.GetClaimValue("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider");

                // LookUp User and set the session variables
                var userList = new List<User>(unitOfWork.GetRepository<User>().GetAll());
                var user = userList.Find(u => u.Profile.ParameterList.Find(p => p.Name == identityprovider && p.Value == nameidentifier) != null);

                // If user is not already registered in the system redirect to register page
                if (user != null)
                {
                    SessionFacade.UserId = user.Id;
                    SessionFacade.UserIsActive = user.IsActive;
                    SessionFacade.UserRole = user.Role;                
                }
                else
                    page.Response.Redirect("/Profile.aspx");
            }

            // If user is not active redirect to waiting page
            if (SessionFacade.UserIsActive == false)
            {
                //TODO needs to be changed to point to site with correct text 
                page.Response.Redirect("/Error.aspx?e=notactivated");
            }
        }

        public static void ActivateUser(IUnitOfWork unitOfWork, User user)
        {
            // Activate user
            user.IsActive = true;
            unitOfWork.Save(true);

            // For self validation in testing mode
            if (SessionFacade.UserId == user.Id)
                SessionFacade.UserIsActive = true;
        }

        public static void LogOut(Page page)
        {            
            if (page.User.Identity.IsAuthenticated)
            {
                // LogOut from ACS Security
                var wsFederationAuthenticationModule = FederatedAuthentication.WSFederationAuthenticationModule;
                wsFederationAuthenticationModule.SignOut(true);
                page.Session.Abandon();                
                page.Response.Redirect("/Login.aspx", false);
            }
        }
    }
}
